package io.xccit.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xccit.model.system.SysRole;
import io.xccit.model.system.SysUserRole;
import io.xccit.system.mapper.ISysRoleMapper;
import io.xccit.system.mapper.ISysUserRoleMapper;
import io.xccit.system.service.ISysUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author CH_ywx
 * @date 2023-06-25
 * @description
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<ISysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Resource
    private ISysUserRoleMapper sysUserRoleMapper;
    @Resource
    private ISysRoleMapper sysRoleMapper;
    @Override
    public List<SysRole> getRoleByUserID(Long userID) {
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId,userID);
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(wrapper);
        List<Long> hadRoleIDS;//用户拥有的角色的ID集合
        List<SysRole> roleList = null;//根据角色ID查询的角色
        if (!CollectionUtils.isEmpty(sysUserRoles)){
            hadRoleIDS = sysUserRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
            roleList= sysRoleMapper.selectBatchIds(hadRoleIDS);
        }else{
            hadRoleIDS = new ArrayList<>();//防止报错
            roleList = new ArrayList<>();
            SysRole role = new SysRole();
            role.setRoleName("暂时没有角色信息");
            roleList.add(role);
        }
        return roleList;
    }
}
